package com.platform.modules.wallet.controller;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.StrUtil;
import com.platform.common.enums.YesOrNoEnum;
import com.platform.common.exception.BaseException;
import com.platform.common.shiro.ShiroUtils;
import com.platform.common.version.ApiVersion;
import com.platform.common.version.VersionEnum;
import com.platform.common.web.domain.AjaxResult;
import com.platform.modules.chat.domain.ChatGroup;
import com.platform.modules.chat.domain.ChatUser;
import com.platform.modules.chat.service.ChatGroupService;
import com.platform.modules.chat.service.ChatUserService;
import com.platform.modules.wallet.domain.Wallet;
import com.platform.modules.wallet.domain.WalletRedPacket;
import com.platform.modules.wallet.domain.WalletRedPacketSub;
import com.platform.modules.wallet.enums.IdentityStatusEnum;
import com.platform.modules.wallet.enums.RedPacketTypeEnum;
import com.platform.modules.wallet.enums.WalletStatusEnum;
import com.platform.modules.wallet.service.WalletRedPacketService;
import com.platform.modules.wallet.service.WalletService;
import com.platform.modules.wallet.vo.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 钱包-红包
 */
@RestController
@Slf4j
@RequestMapping("/wallet")
public class RedPacketController {

    @Resource
    private ChatUserService chatUserService;
    @Resource
    private WalletRedPacketService walletRedPacketService;
    @Resource
    private WalletService walletService;
    @Resource
    private ChatGroupService groupService;

    /**
     * 查询红包信息
     */
    @ApiVersion(VersionEnum.V1_0_0)
    @GetMapping("/getRedPacket/{queryId}")
    public AjaxResult getRedPacket(@PathVariable String queryId) {
        WalletRedPacket redPacket = walletRedPacketService.getByQueryId(queryId);
        return AjaxResult.success(walletRedPacketService.findById(redPacket.getId()));
    }

    /**
     * 查询是否抢过红包
     */
    @ApiVersion(VersionEnum.V1_0_0)
    @GetMapping("/hasRedPacket/{queryId}")
    public AjaxResult hasRedPacket(@PathVariable String queryId) {

        Long userId = ShiroUtils.getUserId();

        WalletRedPacket redPacket = walletRedPacketService.getByQueryId(queryId);

        return AjaxResult.success(walletRedPacketService.findToIdByUserId(redPacket.getId(), userId));
    }

    /**
     * 查询是否还有可抢红包
     */
    @ApiVersion(VersionEnum.V1_0_0)
    @GetMapping("/haveRedPacket/{queryId}")
    public AjaxResult haveRedPacket(@PathVariable String queryId) {

        WalletRedPacket redPacket = walletRedPacketService.getByQueryId(queryId);
        List<WalletRedPacketSub> walletRedPacketSubs = walletRedPacketService.querySubInitList(redPacket.getId());

        return AjaxResult.success(walletRedPacketSubs.size() > 0);
    }

    /**
     * 查询是否还有可抢红包
     */
    @ApiVersion(VersionEnum.V1_0_0)
    @GetMapping("/haveRedPacket1/{queryId}")
    public AjaxResult haveRedPacket1(@PathVariable String queryId) {

        WalletRedPacket redPacket = walletRedPacketService.getByQueryId(queryId);

        if (RedPacketTypeEnum.GROUP.equals(redPacket.getType())) {

            ChatGroup group = groupService.getById(redPacket.getToId());

            Long userId = ShiroUtils.getUserId();

            if (YesOrNoEnum.YES.equals(group.getWhite())){
                if (!groupService.isMasterOrManage(group, userId)){
                    if (StrUtil.isNotBlank(group.getWhiteIds())){
                        String[] whiteIds = group.getWhiteIds().split(",");
                        if (!Arrays.asList(whiteIds).contains(String.valueOf(userId))){
                            return AjaxResult.success("2");
                        }
                    }
                }
            }

            if (StrUtil.isNotBlank(redPacket.getToIds())) {
                String[] toIds = redPacket.getToIds().split(",");
                if (!Arrays.asList(toIds).contains(String.valueOf(userId))) {
                    return AjaxResult.success("2");
                }
            }
        }

        List<WalletRedPacketSub> walletRedPacketSubs = walletRedPacketService.querySubInitList(redPacket.getId());
        if (walletRedPacketSubs.size() > 0) {
            return AjaxResult.success("1");
        } else {
            return AjaxResult.success("0");
        }
    }

    /**
     * 开红包
     */
    @ApiVersion(VersionEnum.V1_0_0)
    @PostMapping("/openRedPacket")
    public AjaxResult openRedPacket(@Validated @RequestBody RedPacketVo02 vo) {

        Long userId = ShiroUtils.getUserId();

        try {
            return AjaxResult.success(walletRedPacketService.openRedPacket(vo.getId(), userId));
        } catch (BaseException e) {
            return AjaxResult.fail(e.getMessage());
        }

    }

    /**
     * 查询已接收的红包列表
     */
    @ApiVersion(VersionEnum.V1_0_0)
    @GetMapping("/queryRedPacket/{queryId}")
    public AjaxResult queryRedPacket(@PathVariable String queryId) {

        WalletRedPacket redPacket = walletRedPacketService.getByQueryId(queryId);

        RedPacketVo04 redPacketVo04 = Convert.convert(RedPacketVo04.class, redPacket);

        List<RedPacketVo03> list = new ArrayList<>();

        List<WalletRedPacketSub> walletRedPacketSubs = walletRedPacketService.querySubSuccessList(redPacket.getId());
        for (WalletRedPacketSub walletRedPacketSub : walletRedPacketSubs) {
            RedPacketVo03 convert = Convert.convert(RedPacketVo03.class, walletRedPacketSub);
            ChatUser to = chatUserService.findById(convert.getToId());
            UserVo toVO = Convert.convert(UserVo.class, to);
            convert.setTo(toVO);
            list.add(convert);
        }

        redPacketVo04.setSubVoList(list);

        return AjaxResult.success(redPacketVo04);
    }
}
